package com.zust.ysc012.service;

import com.zust.ysc012.entity.Table;

import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.zust.ysc012.dao.PublicDao.*;
import static com.zust.ysc012.dao.StudentDao.*;
import static com.zust.ysc012.dao.TeacherDao.select_teacher_name_sql;
import static com.zust.ysc012.utility.DBUtil.con;
import static com.zust.ysc012.utility.DBUtil.pre;

/**
 * @Description
 * @Author Mato
 * @Date 03/07/2021 19:22
 */
public class TableService {
    public static List<Table> getAllTables() throws SQLException, UnsupportedEncodingException {
        List<Table> tables = new ArrayList<>();
        ResultSet rs;
        String number = getNowUser();
        rs = select_table_student_sql();
        while (rs.next()) {
            if (rs.getString(2).equals(number) || rs.getString(4).equals(number)){
                Table table = new Table();
                table.setProject_name(select_project_name(rs.getInt(3)));
                table.setProject_number(rs.getInt(3));
                table.setStatus(rs.getInt(5));
                table.setStudent_name(select_student_name_sql(rs.getString(2)));
                table.setStudent_number(rs.getString(2));
                table.setTeacher_name(select_teacher_name_sql(rs.getString(4)));
                table.setTeacher_number(rs.getString(4));
                tables.add(table);
            }
        }
        pre.close();;
        con.close();
        return tables;
    }

    public static void alertStatus1(int project_number) throws SQLException {
        alert_status1_sql(project_number);
    }

    public static void alertStatus2(int project_number) throws SQLException {
        alert_status2_sql(project_number);
    }
}
